package com.service.impl;

import com.dao.FilmMapper;
import com.dao.OrderMapper;
import com.dao.PlayMapper;
import com.entity.Film;
import com.entity.Order;
import com.entity.Play;
import com.google.gson.Gson;
import com.service.OrderService;
import com.vo.OrderDetailVO;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private PlayMapper playMapper;

    @Autowired
    private FilmMapper filmMapper;


    @Override
    public List<Object> addOrder(String playId, int cnt, String seatStr) {
        Order order = new Order();
        // 生成id的随机算法
        // 当前时间+id+用户id
        //  需要验证id的唯一性  即增加前去查询一下
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(new Date()).replaceAll("[^\\d]", "");
        order.setOrderId(dateStr+playId);
        order.setPlayId(playId);
        order.setSeatNum(cnt);

        Play play = playMapper.selectByPlayId(playId);
        BigDecimal price = play.getPrice();
        BigDecimal allPrice = new BigDecimal(cnt).multiply(price);

        order.setPrice(allPrice);

        long time = (filmMapper.filmDetail(play.getfId()).getF_length())*60*1000;
        try {
            String date = format.format(play.getpTime().getTime()+time);
            order.setCreateTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> seatList = new ArrayList<>();
        String arr[] = seatStr.split("号");
        for (String one : arr ) {
            one = one+"号" ;
            seatList.add(one);
        }
        order.setSeat(new Gson().toJson(seatList));
        order.setState("未支付");

        List<Object> list = new ArrayList<>();
        list.add(orderMapper.insert(order));
        list.add(order.getOrderId());
        return list;
    }


    @Override
    public List<String> getSeatsByPlayId(String playId) {
        List<Order> orderList = orderMapper.getOrdersByPlayId(playId);
        List<String> seats = new ArrayList<>();
        // 遍历订单  以及 每笔订单中的 座位信息
        // 将已售出的所有座位  总集合返回
        for (Order order : orderList) {
            List<String> seatList = new Gson().fromJson(order.getSeat(),List.class);
            for (String s : seatList) {
                seats.add(s);
            }
        }
        return seats;
    }

    @Override
    public int updateByState(String orderId, String state) {
        System.out.println(orderId+"+"+state);
        Map<String,String> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("state",state);
        return orderMapper.updateByState(map);
    }

    @Override
    public OrderDetailVO getOrdersByOrderId(String orderId) {
        OrderDetailVO orderVO = new OrderDetailVO();
        Order order =orderMapper.getOrdersByOrderId(orderId);
        orderVO.setOrderId(order.getOrderId());
        orderVO.setPrice(order.getPrice());
        orderVO.setSeatNum(order.getSeatNum());

//        List<String> seats = new ArrayList<>();
        List<String> seatList = new Gson().fromJson(order.getSeat(),List.class);
        orderVO.setSeatStr(seatList);

        Play play = playMapper.selectByPlayId(order.getPlayId());

        orderVO.setPid(play.getpId());
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        StringBuffer dateStr = new StringBuffer(format.format(play.getpTime()));
        dateStr.replace(2,3,"月");
        dateStr.replace(5,6,"日 ");
        orderVO.setPTime(dateStr.toString());

        orderVO.setRoomName(play.getRoomName());
        orderVO.setLanType(play.getLanType());

        Film film = filmMapper.filmDetail(play.getfId());
        orderVO.setFName(film.getF_name());
        orderVO.setLength(film.getF_length());
        orderVO.setImgPath(film.getImg_path());


        return orderVO;
    }


}
